import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author gao
 * @date 2025/7/26 16:29
 */

// 固定线程数目的线程池
class MyFixedThreadPool {
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
    public MyFixedThreadPool(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        MyFixedThreadPool threadPool = new MyFixedThreadPool(4);
        for (int i = 0; i < 100; i++) {
            int id = i;
            threadPool.submit(() -> {
                System.out.println("执行任务：" + id);
            });
        }
    }
}